<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use app\common\library\Token;
use think\Session;
use think\Request;

class My extends Frontend {

    public function _initialize() {
        parent::_initialize();
        $this->assign('menu', 3);
    }

    public function index() {
        $userInfo = \think\Db::name('user')->where(array('user_id' => $this->iUserId))->find();

        $carInfo = \think\Db::name('user_car')->where(array('user_id' => $this->iUserId))->find();

        $walletInfo = \think\Db::name('wallet')->where(array('user_id' => $this->iUserId))->find();

        if (!empty($carInfo)) {
            $this->assign('user_car', $carInfo);
        }

        if (!empty($walletInfo)) {
            $this->assign('wallet', $walletInfo);
        }
        $this->assign('user', $userInfo);
        return $this->view->fetch();
    }

    public function carInfo() {
        $carInfo = \think\Db::table('fa_user_car')
                ->alias('uc')
                ->field('uc.*,cb.brand_name,cc.color_name,ct.type_name,u.province,u.city,u.area')
                ->join('fa_user u', 'uc.user_id = u.user_id')
                ->join('fa_car_brand cb', 'uc.car_brand_id = cb.car_brand_id')
                ->join('fa_car_color cc', 'cc.car_color_id = cc.car_color_id')
                ->join('fa_car_type ct', 'ct.car_type_id = ct.car_type_id')
                ->where(array('uc.user_id' => $this->iUserId))
                ->find();

        if (!empty($carInfo)) {
            $carInfo['driving_license_img'] = explode(',', $carInfo['driving_license_img']);
        }
        $this->assign('user_car', $carInfo);
        return $this->view->fetch();
    }

    public function setInfo() {

        $userInfo = \think\Db::name('user')->where(array('user_id' => $this->iUserId))->find();

        $this->assign('user', $userInfo);
        return $this->view->fetch();
    }

    public function carAuth() {

        if (Request::instance()->isPost()) {
            $car_id = input('car_id', 0);
            $car_brand_id = input('car_brand_id', 0);
            $car_type_id = input('car_type_id', 0);
            $car_color_id = input('car_color_id', 0);
            $city_name = input('city_name');
            $js_city = input('js_city');
            $remark = input('remark');

            $driving_license_img[] = input('driving_license_img1');
            $driving_license_img[] = input('driving_license_img2');

            $data['car_brand_id'] = $car_brand_id;
            $data['car_type_id'] = $car_type_id;
            $data['car_color_id'] = $car_color_id;
            $data['remark'] = $remark;
            $data['createtime'] = time();

            $data['driving_license_img'] = implode(',', $driving_license_img);

            $city_name = explode(',', $city_name);
            if (isset($city_name[0])) {
                $userinfo['province'] = $city_name[0];
            }

            if (isset($city_name[1])) {
                $userinfo['city'] = $city_name[1];
            }

            if (isset($city_name[2])) {
                $userinfo['area'] = $city_name[2];
            }

            $userinfo['js_city'] = $js_city;


            $res = 0;
            if ($car_id == 0) {
                $data['user_id'] = $this->iUserId;
                $res = \think\Db::name('user_car')->insert($data);
            } else {
                $data['car_cert'] = 0;
                $res = \think\Db::name('user_car')->where(array('car_id' => $car_id, 'user_id' => $this->iUserId))->update($data);
            }

            if ($res) {
                $res = \think\Db::name('user')->where(array('user_id' => $this->iUserId))->update($userinfo);
                $this->redirect('My/index');
            }
        } else {


            $brand_list = \think\Db::name('car_brand')->field('car_brand_id as value,brand_name as text')->order('createtime', 'desc')->select();
            $type_list = \think\Db::name('car_type')->field('car_type_id as value,type_name as text')->order('createtime', 'desc')->select();
            $color_list = \think\Db::name('car_color')->field('car_color_id as value,color_name as text')->order('createtime', 'desc')->select();


            $this->assign('brand_list', json_encode($brand_list));
            $this->assign('type_list', json_encode($type_list));
            $this->assign('color_list', json_encode($color_list));

            $carInfo = \think\Db::table('fa_user_car')
                    ->alias('uc')
                    ->field('uc.*,cb.brand_name,cc.color_name,ct.type_name,u.province,u.city,u.area,u.js_city')
                    ->join('fa_user u', 'uc.user_id = u.user_id')
                    ->join('fa_car_brand cb', 'uc.car_brand_id = cb.car_brand_id')
                    ->join('fa_car_color cc', 'cc.car_color_id = cc.car_color_id')
                    ->join('fa_car_type ct', 'ct.car_type_id = ct.car_type_id')
                    ->where(array('uc.user_id' => $this->iUserId))
                    ->find();

            if (!empty($carInfo)) {
                $carInfo['driving_license_img'] = explode(',', $carInfo['driving_license_img']);
            }else{
                $carInfo['car_id'] = 0;
                $carInfo['car_brand_id'] = 0;
                $carInfo['car_color_id'] = 0;
                $carInfo['car_type_id'] = 0;
                $carInfo['brand_name'] = '';
                $carInfo['color_name'] = '';
                $carInfo['type_name'] = '';
                $carInfo['province'] = '';
                $carInfo['city'] = '';
                $carInfo['area'] = '';
                $carInfo['js_city'] = '';
                $carInfo['remark'] = '';
                $carInfo['driving_license_img'][0] = '';
                $carInfo['driving_license_img'][1] = '';
            }
            
            if(empty($carInfo['province'])&&empty($carInfo['city'])&&empty($carInfo['area'])){
                $carInfo['juzhudi'] = '';
            }else{
               $carInfo['juzhudi'] = $carInfo['province'].','.$carInfo['city'].','.$carInfo['area']; 
            }
            
            $this->assign('user_car', $carInfo);

            return $this->view->fetch();
        }
    }

    public function set() {
        $s = input('s', 1);

        if (Request::instance()->isPost()) {
            $keywords = input('keywords', '');
            $data = [];

            if ($s == 1) {
                $data['nickname'] = $keywords;
            }

            if ($s == 2) {
                $data['industry'] = $keywords;
            }

            if ($s == 3) {
                $data['company'] = $keywords;
            }

            $res = \think\Db::name('user')->where(array('user_id' => $this->iUserId))->update($data);

            if ($res) {
                $this->redirect('My/setInfo');
            }
        } else {

            $map[1] = '请填写新昵称,12字内';
            $map[2] = '请填写您所在的行业';
            $map[3] = '请填写您的公司名称';

            $userInfo = \think\Db::name('user')->where(array('user_id' => $this->iUserId))->find();

            $value = '';

            if ($s == 1) {
                $value = $userInfo['nickname'];
            }

            if ($s == 2) {
                $value = $userInfo['industry'];
            }

            if ($s == 3) {
                $value = $userInfo['company'];
            }

            $this->assign('s', $s);
            $this->assign('value', $value);
            $this->assign('placeholder', $map[$s]);
            return $this->view->fetch();
        }
    }

    public function noAuth() {
        return $this->view->fetch();
    }

    public function setuserinfo() {
        $avatar = input('avatar', '');

        $sex = input('sex', -1);

        if ($avatar) {
            $data['avatar'] = $avatar;
        }

        if ($sex != -1) {
            $data['sex'] = $sex;
        }

        $res = \think\Db::name('user')->where(array('user_id' => $this->iUserId))->update($data);

        if ($res) {
            $this->success('修改成功');
        } else {
            $this->error('修改失败');
        }
    }

    public function wallet() {

        $walletInfo = \think\Db::name('wallet')->where(array('user_id' => $this->iUserId))->find();

        if (!empty($walletInfo)) {
            $this->assign('wallet', $walletInfo);
        }

        $wallet_log = \think\Db::table('fa_wallet')
                ->alias('w')
                ->field('wl.*')
                ->join('fa_wallet_log wl', 'w.wallet_id = wl.wallet_id')
                ->where(array('w.user_id' => $this->iUserId))
                ->order('wl.createtime desc')
                ->select();


        $shou = [];
        $zhi = [];
        foreach ($wallet_log as $val) {
            //收入
            if ($val['change_type'] == 0) {
                $val['order_no'] = \think\Db::name('order')->where(array('order_id' => $val['order_id']))->value('order_no');

                $shou[] = $val;
            }

            //支出
            if ($val['change_type'] == 1) {
                if ($val['cash_id'] != 0) {
                    $val['cash_info'] = \think\Db::name('cash')->where(array('cash_id' => $val['cash_id']))->find();
                }

                if ($val['order_id'] != 0) {
                    $val['order_no'] = \think\Db::name('order')->where(array('order_id' => $val['order_id']))->value('order_no');
                }
                $zhi[] = $val;
            }
        }

        $this->assign('shou', $shou);
        $this->assign('zhi', $zhi);
        return $this->view->fetch();
    }

    public function recom() {
        $rurl = url('index/Pub/register', ['rid' => $this->iUserId]);
        $this->assign('rurl', $this->request->domain() . $rurl);
        return $this->view->fetch();
    }

    public function cash() {
        $walletInfo = \think\Db::name('wallet')->where(array('user_id' => $this->iUserId))->find();

        if (Request::instance()->isPost()) {
            $account = input('account');
            $name = input('name');
            $amount = input('amount', 0);

            if (empty($account)) {
                $this->error('请输入支付宝账号');
            }
            if (empty($name)) {
                $this->error('请输入支付宝实名');
            }

            if ($account < 100) {
                $this->error('最低提现金额100元');
            }

            if ($walletInfo['cash_amount'] < $amount) {
                $this->error('可提现余额不足');
            }

            $data['cash_account'] = $account;
            $data['cash_name'] = $name;
            $data['amount'] = $amount;
            $data['user_id'] = $this->iUserId;
            $data['createtime'] = time();

            $res = \think\Db::name('cash')->insert($data);
            $cash_id = \think\Db::name('cash')->getLastInsID();

            $wallet_res = \think\Db::name('wallet')->where(['wallet_id' => $walletInfo['wallet_id'], 'user_id' => $this->iUserId])->setDec('cash_amount', $amount);

            $log['wallet_id'] = $walletInfo['wallet_id'];
            $log['change_amount'] = $amount;
            $log['change_type'] = 1;
            $log['change_matters'] = 4;
            $log['cash_id'] = $cash_id;
            $log['createtime'] = time();
            
            $log_res = \think\Db::name('wallet_log')->insert($log);


            $this->success('提现申请成功', url('My/wallet'));
        } else {
            $cashInfo = \think\Db::name('cash')->where(array('user_id' => $this->iUserId))->order('cash_id desc')->find();

            if (!empty($walletInfo)) {
                $this->assign('wallet', $walletInfo);
            }

            if (empty($cashInfo)) {
                $cashInfo['cash_account'] = '';
                $cashInfo['cash_name'] = '';
            }
            $this->assign('cashInfo', $cashInfo);

            return $this->view->fetch();
        }
    }

}
